package com.sfdk.admin.helpers
{
	import com.sfdk.admin.ApplicationData;
	import com.sfdk.admin.data.api.IDataSource;
import com.sfdk.data.api.ITreeDataProvider;
import com.sfdk.events.IOEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	
	/**
	 * Utility class used to read data from directory on the server
	 */
	public class FileReader extends EventDispatcher implements IDataSource
	{
        public function FileReader(moduleName : String, fileType : String, dataClass : Class, fileFilter : String, classParameter : * = null, classParameter2 : * = null)
		{
			_isLoaded = false;
			_moduleName = moduleName;
			_fileType = fileType;
			_dataClass = dataClass;
            _fileFilter = fileFilter;
            _classParameter = classParameter;
            _classParameter2 = classParameter2;
			loadData();
		}
		
		
		protected var _classParameter : *;
        protected var _classParameter2 : *;
		protected var _moduleName : String;
		protected var _fileType : String;
		protected var _dataClass : Class;
        protected var _fileFilter:String;

		protected var _toLoad : int;
		
		protected var _isLoaded : Boolean;
		
		public function get isLoaded():Boolean
		{
			return _isLoaded;
		}
		
		public function set isLoaded(value:Boolean):void
		{
			_isLoaded = value;
		}
		
		protected var _data : IList;
		
		public function get data():IList
		{
			return _data;
		}
		
		public function set data(value:IList):void
		{
			_data = value;
		}
		
		public function refreshData() : void
		{
			_isLoaded = false;
			loadData();
		}
		
		protected function loadData() : void
		{
			var dataURL : String = ApplicationData.BASE_LOCATION+_moduleName+"discovery.php?target="+_fileType+"&filter="+_fileFilter;
			var dataRequest : URLRequest = new URLRequest(dataURL);
			var dataLoader : URLLoader = new URLLoader(dataRequest);
			dataLoader.addEventListener(Event.COMPLETE, onDirectoryListed, false, 0, true);
		}
		
		protected function onDirectoryListed(event : Event):void
		{
			var fileListString : String = event.target.data;
			var data : ArrayCollection = new ArrayCollection();
			if(fileListString != "")
			{
				var fileList : Array = fileListString.split(":");
				var currentFile : ITreeDataProvider;
				_toLoad = fileList.length;
				
				for (var i:int = 0; i < fileList.length; i++)
				{
					if(fileList[i] != "")
					{
                        if(_classParameter != null)
						    currentFile = new _dataClass(ApplicationData.BASE_LOCATION+_moduleName+_fileType+"/", fileList[i], _classParameter, _classParameter2);
                        else
                            currentFile = new _dataClass(ApplicationData.BASE_LOCATION+_moduleName+_fileType+"/", fileList[i]);
						currentFile.addEventListener(IOEvent.LOAD_COMPLETE, onFileLoaded, false, 0, true);
					
						data.addItem(currentFile);
					}
					else
						_toLoad--;
				}
				_data = data;
			}
			else
			{
				_data = data;
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		private function onFileLoaded(e : Event) : void
		{
			_toLoad--;
			if(_toLoad == 0)
			{
				dispatchEvent(new Event(Event.COMPLETE));
				_isLoaded = true;
			}
		}
	}
	
	
}